import atomForConfig from './atomForConfig'
import { reviewInfoAtom } from './reviewInfoAtom'
import { DISMISS_START_CARD_DATE_KEY, defaultFontSizeConfig } from '@/constants'
import { idDictionaryMap } from '@/resources/dictionary'
import { correctSoundResources, keySoundResources, wrongSoundResources } from '@/resources/soundResource'
import type {
  Dictionary,
  InfoPanelState,
  LoopWordTimesOption,
  PhoneticType,
  PronunciationType,
  WordDictationOpenBy,
  WordDictationType,
} from '@/typings'
import type { ReviewRecord } from '@/utils/db/record'
import { atom } from 'jotai'
import { atomWithStorage } from 'jotai/utils'

export const currentDictIdAtom = atomWithStorage('currentDict', 'cet4') // 默认值为 cet4保存到localStorage
export const currentDictInfoAtom = atom<Dictionary>((get) => {
  const id = get(currentDictIdAtom)
  let dict = idDictionaryMap[id]
  // 如果 dict 不存在，则返回 cet4. Typing 中会检查 DictId 是否存在，如果不存在则会重置为 cet4
  if (!dict) {
    dict = idDictionaryMap.cet4
  }
  return dict
})

export const currentChapterAtom = atomWithStorage('currentChapter', 0) // 当前章节，默认值为0，保存到localStorage

export const loopWordConfigAtom = atomForConfig<{ times: LoopWordTimesOption }>('loopWordConfig', {
  times: 1,
})

export const keySoundsConfigAtom = atomForConfig('keySoundsConfig', {
  isOpen: true, // 是否开启按键音效
  isOpenClickSound: true, // 是否开启按键音效
  volume: 1, // 按键音效音量
  resource: keySoundResources[0], // 按键音效资源
})

export const hintSoundsConfigAtom = atomForConfig('hintSoundsConfig', {
  // 提示音配置
  isOpen: true, // 是否开启提示音
  volume: 1, // 提示音音量
  isOpenWrongSound: true, // 是否开启错误音效
  isOpenCorrectSound: true, // 是否开启正确音效
  wrongResource: wrongSoundResources[0], // 错误音效资源
  correctResource: correctSoundResources[0], // 正确音效资源
})

export const pronunciationConfigAtom = atomForConfig('pronunciation', {
  isOpen: true, // 是否开启单词发音
  volume: 1, // 发音音量
  type: 'us' as PronunciationType, // 发音类型 美音 英音
  name: '美音', // 发音名称
  isLoop: false, // 是否循环发音
  isTransRead: false, // 是否开启释义朗读
  transVolume: 1, // 释义朗读音量
  rate: 1, // 朗读速度
})

export const fontSizeConfigAtom = atomForConfig('fontsize', defaultFontSizeConfig)

export const pronunciationIsOpenAtom = atom((get) => get(pronunciationConfigAtom).isOpen)

export const pronunciationIsTransReadAtom = atom((get) => get(pronunciationConfigAtom).isTransRead)

export const randomConfigAtom = atomForConfig('randomConfig', {
  // 是否随机单词
  isOpen: false,
})

export const isShowPrevAndNextWordAtom = atomWithStorage('isShowPrevAndNextWord', true)

export const isIgnoreCaseAtom = atomWithStorage('isIgnoreCase', true)

export const isShowAnswerOnHoverAtom = atomWithStorage('isShowAnswerOnHover', true)

export const isTextSelectableAtom = atomWithStorage('isTextSelectable', false)

export const reviewModeInfoAtom = reviewInfoAtom({
  isReviewMode: false,
  reviewRecord: undefined as ReviewRecord | undefined,
}) // 复习模式信息
export const isReviewModeAtom = atom((get) => get(reviewModeInfoAtom).isReviewMode) // 是否为复习模式

export const phoneticConfigAtom = atomForConfig('phoneticConfig', {
  // 音标配置
  isOpen: true, // 是否开启音标
  type: 'us' as PhoneticType, // 音标类型 美式 英式
})

export const isOpenDarkModeAtom = atomWithStorage('isOpenDarkModeAtom', window.matchMedia('(prefers-color-scheme: dark)').matches)

export const isShowSkipAtom = atom(false)

export const isInDevModeAtom = atom(false)

export const infoPanelStateAtom = atom<InfoPanelState>({
  donate: false,
  vsc: false,
  community: false,
  redBook: false,
})

export const wordDictationConfigAtom = atomForConfig('wordDictationConfig', {
  // 单词听写配置
  isOpen: false, // 是否开启单词听写
  type: 'hideAll' as WordDictationType, // 听写类型 隐藏全部 隐藏字母 隐藏释义
  openBy: 'auto' as WordDictationOpenBy, // 听写开启方式 自动 手动
})

export const dismissStartCardDateAtom = atomWithStorage<Date | null>(DISMISS_START_CARD_DATE_KEY, null)

// Enhanced version promotion popup state
export const hasSeenEnhancedPromotionAtom = atomWithStorage('hasSeenEnhancedPromotion', false) // 用户是否见过增强版推广

// for dev test
//   dismissStartCardDateAtom = atom<Date | null>(new Date())
